import { getRandomPropName, deepclone } from "@/utils"
export class CopyWidgetToContainerCommand {
  constructor(widget, widgetList, widgetMap) {
    this.widget = deepclone(widget)
    this.widgetList = widgetList
    this.widgetMap = widgetMap
  }
  execute () {
    this.copySelf(this.widget)
    this.widgetList.push(this.widget)
  }
  undo () {
    this.widgetList.pop()
    this.cleanMap(this.widget)
  }
  copySelf (widget) {
    const target = widget.cols ?? widget.widgetList ?? []
    const randomPropName = getRandomPropName(widget.type)
    widget.options.propName = randomPropName
    widget.originConfig.propName = randomPropName
    this.widgetMap.set(randomPropName, widget)
    for (let i of target) {
      this.copySelf(i)
    }
  }
  cleanMap(widget) {
    this.widgetMap.delete(widget.options.propName)
    const target = widget.cols ?? widget.widgetList ?? []
    for (let i of target) {
      this.cleanMap(i)
    }
  }
}